{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lab: Predicting Life Expectancy from BMI in Countries using Linear Regression\n",
    "\n",
    "\n",
    "In this lab, you'll be working with data on the average life expectancy at birth and the average BMI for males across the world. The data comes from [Gapminder](https://www.gapminder.org/).\n",
    "\n",
    "The data file can be found in the \"bmi_and_life_expectancy.csv\" file. It includes three columns, containing the following data:\n",
    "* **Country** – The country the person was born in.  \n",
    "* **Life expectancy** – The average life expectancy at birth for a person in that country.\n",
    "* **BMI** – The mean BMI of males in that country.\n",
    "\n",
    "### You'll need to complete each of the following steps:\n",
    "\n",
    "**1. Load the data**\n",
    "\n",
    "**2. Build a linear regression model**\n",
    "\n",
    "**3. Predict using the model**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. Load and plot the data\n",
    "* The data is in the file called \"bmi_and_life_expectancy.csv\".\n",
    "* Use pandas [`read_csv`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html) to load the data into a dataframe.\n",
    "* Assign the dataframe to the variable `bmi_life_data`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# TODO: Load the data in Pandas\n",
    "\n",
    "bmi_life_data = None\n",
    "\n",
    "# Print the data\n",
    "bmi_life_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some helper functions:\n",
    "- One to plot the data.\n",
    "- One to plot any line, given the slope $m$ and the y-intercept $b$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "x = np.array(bmi_life_data[[\"BMI\"]])\n",
    "y = np.array(bmi_life_data[\"Life expectancy\"])\n",
    "\n",
    "def draw_data(x, y):\n",
    "    for i in range(len(x)):\n",
    "        plt.scatter(x[i], y[i], color='blue', edgecolor='k')\n",
    "    plt.xlabel('BMI')\n",
    "    plt.ylabel('Life expectancy')\n",
    "\n",
    "def display(m, b, color='g'):\n",
    "    r = np.arange(min(x), max(x), 0.1)\n",
    "    plt.plot(r, m*r+b, color)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Plotting the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "draw_data(x, y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. Build a Linear Regression Model\n",
    "* Create a regression model and assign the weights to the array `bmi_life_model`.\n",
    "* Fit the model to the data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "epochs = 1000\n",
    "learning_rate = 0.001\n",
    "\n",
    "# TODO: Finish the code for this function\n",
    "def linear_regression(x, y):\n",
    "    # Initialize m and b\n",
    "    m=1\n",
    "    b=0\n",
    "    # TODO: Use the square trick to update the weights\n",
    "    # and run it for a number of epochs\n",
    "    return(m, b)\n",
    "m, b = linear_regression(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "linear_regression(x,y)\n",
    "draw_data(x, y)\n",
    "display(m[0], b[0])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. Predict using the Model\n",
    "* Predict using a BMI of 21.07931 and assign it to the variable `laos_life_exp`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# TODO: Write the prediction function\n",
    "def predict(m, b, bmi):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
